package GUILayer;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import CtrLayer.CtrEmployee;
import ModelLayer.Employee;

import java.util.Timer;
import java.util.TimerTask;


public class MainWindow extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JFrame frameApartments;
	private JTabbedPane tabbedPanel;
	
	private JPanel[] jTabPanels;
	private ImageIcon[] iconsMenu;
	private String[] tabNames,iconNames;
	private static JLabel errorMessages;
	private static int employeeID;
	private static Employee loginEmployee;
	private CtrEmployee ctrEmployee;
	private boolean hasFullAccess;
	
	final int iconTabSize = 25;
	
	public MainWindow(int employeeid) 
	{
		employeeID = employeeid;
		ctrEmployee = new CtrEmployee();
		loginEmployee = ctrEmployee.getEmployee_byEmployeeID(employeeID);
		
		if(loginEmployee.getPosition().equals("Operations Manager") || loginEmployee.getPosition().equals("IT Technician") || loginEmployee.getPosition().equals("Portfolio Manager") || loginEmployee.getPosition().equals("Director")) hasFullAccess = true;
		
		initComponentsFrame();
	}
	
	public static int getEmployeeId(){
		return employeeID;
	}
	
	public static void setError(String error){
		errorMessages.setText(error);
		
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				resetError();
			}
		}, 4000);
	}
	
	private static void resetError(){
		errorMessages.setText(null);
	}
	
	
	private void initComponentsFrame() {
		frameApartments = new JFrame();
		tabbedPanel = new JTabbedPane();
		int tabFields = 8;
		
		if(!hasFullAccess) tabFields--;
		
		//======== tabNames & iconNames ========
		{
			tabNames = new String[tabFields];
			iconNames = new String[tabFields];
			tabNames[0] = "Dashboard";  iconNames[0] = "home";
			tabNames[1] = "Apartments"; iconNames[1] = "apartment";
			tabNames[2] = "Landlords";  iconNames[2] = "peoplegreen";
			tabNames[3] = "Tenants";	iconNames[3] = "peoplepurple";
			tabNames[4] = "Contracts";	iconNames[4] = "contract";
			tabNames[5] = "Payments";	iconNames[5] = "payment";
			tabNames[6] = "Buildings";	iconNames[6] = "building";
			if(hasFullAccess) {tabNames[7] = "Employees";	iconNames[7] = "people";}
		}
		
		//======== jTabPanels ========
		{
			jTabPanels = new JPanel[tabFields];
			jTabPanels[0] = (new DashboardGUI()).getPanel();
			jTabPanels[1] = (new ApartmentGUI()).getPanel();
			jTabPanels[2] = (new LandlordGUI()).getPanel();
			jTabPanels[3] = (new TenantsGUI()).getPanel();
			jTabPanels[4] = (new ContractGUI()).getPanel();
			jTabPanels[5] = (new PaymentGUI()).getPanel();
			jTabPanels[6] = (new BuildingGUI()).getPanel();
			if(hasFullAccess) {jTabPanels[7] = (new EmployeeGUI()).getPanel();}
		}
		
		//======== iconsMenu ========
		{
			iconsMenu = new ImageIcon[tabFields];
			
			for(int i=0;i<tabFields;i++)
			{
				iconsMenu[i] = new ImageIcon(getClass().getResource("/icons/"+iconNames[i]+".png")){
					private static final long serialVersionUID = 1L;

					@Override
				    public void paintIcon( Component c, Graphics g, int x, int y ) {
				        g.drawImage(getImage(), x, y, iconTabSize, iconTabSize, c);
				    }
				};
			}			
		}
				

		//======== tabbedPanel ========
		tabbedPanel.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				frameApartments.setTitle(tabNames[tabbedPanel.getSelectedIndex()]+" - ApartmentBox Property Management System - Group1 - 2014");
				
				if(tabbedPanel.getSelectedIndex() == 0)
				{
					tabbedPanel.setComponentAt(0, (new DashboardGUI()).getPanel());
				}
			}
		});
		
		tabbedPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		tabbedPanel.setFont(new Font("Verdana", Font.PLAIN, 11));
		
		for(int i=0;i<tabFields;i++)
			tabbedPanel.addTab(tabNames[i], iconsMenu[i], jTabPanels[i]);
		
		
		
		//======== frameApartments ========
		frameApartments.setIconImage(new ImageIcon(getClass().getResource("/icons/logoSign.png")).getImage());
		frameApartments.setMinimumSize(new Dimension(1000,650));
		frameApartments.setPreferredSize(new Dimension(1000,650));
		frameApartments.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frameApartments.setLayout(new GridLayout(0,1));
		
		JPanel mainW = new JPanel(new BorderLayout());
		errorMessages = new JLabel();
		errorMessages.setHorizontalAlignment(JLabel.RIGHT);
		errorMessages.setForeground(Color.red);
		
		mainW.add(tabbedPanel, BorderLayout.CENTER);
		mainW.add(errorMessages, BorderLayout.SOUTH);
		
		Container frameApartmentsContentPane = frameApartments.getContentPane();
		frameApartmentsContentPane.add(mainW);
			
		frameApartments.pack();
		frameApartments.setLocationRelativeTo(frameApartments.getOwner());
		frameApartments.setVisible(true);
		
		
	}
}
